//
//  CategorySelectCell.m
//  quanminzhekou
//
//  Created by zxl on 15/1/12.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "CategorySelectCell.h"

@interface CategorySelectCell ()
@end

@implementation CategorySelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    self.clipsToBounds = YES;
    
    _subCategoryArr = [NSArray new];
    
    _categoryImgView = [[UIImageView alloc] initWithFrame:CGRectMake(11, (48 - 20) / 2.0, 20, 20)];
    [self.contentView addSubview:_categoryImgView];
    
    _titleLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_categoryImgView.frame), CGRectGetMinY(_categoryImgView.frame), 106, 20)];
    _titleLab.font = [UIFont systemFontOfSize:17];
    _titleLab.textColor = [UIColor grayColor];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_titleLab];
    
    _openImgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_titleLab.frame), CGRectGetMinY(_titleLab.frame), 47 / 2.0, 47 / 2.0)];
    _openImgView.image = [UIImage imageNamed:@"category_show_open"];
    _openImgView.hidden = YES;
    [self.contentView addSubview:_openImgView];
    
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, 48, 168, 1)];
    _line.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_line];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 49, 168, 48 * 5)];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.scrollEnabled = NO;
    _tableView.rowHeight = 48;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SubCategorySelectCell class] forCellReuseIdentifier:@"SubCategorySelectCell"];
    [self.contentView addSubview:_tableView];
    
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)setSubCategoryArr:(NSArray *)subCategoryArr {
    if (![_subCategoryArr isEqualToArray:subCategoryArr]) {
        _subCategoryArr = subCategoryArr;
        
        CGRect frame = self.tableView.frame;
        frame.size.height = 48 * _subCategoryArr.count;
        self.tableView.frame = frame;
        
        [self.tableView reloadData];
    }
}

- (void)layoutSubViewData:(NSDictionary*)dic andIsSelect:(BOOL)isSelect {
    self.categoryImgView.image = [UIImage imageNamed:[dic objectForKey:@"img"]];
    self.titleLab.text = [dic objectForKey:@"name"];
    
    BOOL isAll = [self.titleLab.text isEqualToString:@"全部"];
    
    self.selectionStyle = isAll? UITableViewCellSelectionStyleGray: UITableViewCellSelectionStyleNone;
    
    if (isSelect) {
        self.titleLab.textColor = [UIColor redColor];
        self.openImgView.hidden = !isSelect || isAll;
    } else {
        self.titleLab.textColor = [UIColor grayColor];
        self.openImgView.hidden = YES;
    }
    
    self.subCategoryArr = [dic objectForKey:@"item"];
    
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _subCategoryArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SubCategorySelectCell* cell = (SubCategorySelectCell*)[tableView dequeueReusableCellWithIdentifier:@"SubCategorySelectCell"];
    
    NSDictionary* dic = [self.subCategoryArr objectAtIndex:indexPath.row];
    SubCategoryModel* model = [[SubCategoryModel alloc] initWithDic:dic];
    model.pidStr = self.pidStr;
    
    cell.model = model;
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SubCategorySelectCell* cell = (SubCategorySelectCell*)[tableView cellForRowAtIndexPath:indexPath];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOfSelectSubCategoryTitle" object:cell.model];
}

@end
